package com.avic.system.common.convertor;

import com.avic.system.controller.info.MenuTreeInfo;
import com.avic.system.controller.info.UserMenuInfo;
import com.avic.system.controller.info.UserSubMenuInfo;
import com.avic.system.controller.info.SubMenuTreeInfo;
import com.avic.system.dal.model.vo.SubSystemMenu;
import com.avic.system.dal.model.vo.UserSystemMenu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 系统菜单转换去器。PO或者INFO的相关转换
 *
 * @author: huangsheng
 * @date: 2017/10/20
 */
public class SystemMenuConvertor {

    /**
     * 将 systemMenuTrees 转换为 MenuTreeInfo
     *
     * @author: huangsheng
     * @date: 2017/10/20 下午2:00
     */
    public static List<MenuTreeInfo> systemMenuTreeListConInfo(List<UserSystemMenu> systemMenuTrees) {
        if (null != systemMenuTrees && systemMenuTrees.size() > 0) {
            List<MenuTreeInfo> menuTreeInfos = new ArrayList<>(systemMenuTrees.size());
            systemMenuTrees.forEach(systemMenuTree -> {
                //实例化菜单数据
                MenuTreeInfo menuTreeInfo = new MenuTreeInfo(systemMenuTree.getId(), systemMenuTree.getMenuName());
                // 实例化二级菜单
                List<SubSystemMenu> subSystemMenus = systemMenuTree.getSubSystemMenus();
                if (null != subSystemMenus && subSystemMenus.size() > 0) { // 二级菜单循环
                    List<SubMenuTreeInfo> subMenuTreeInfos = new ArrayList<>(subSystemMenus.size());
                    subSystemMenus.forEach(subSystemMenuDO -> {
                        // 设置二级菜单
                        SubMenuTreeInfo subMenuTreeInfo = new SubMenuTreeInfo();
                        subMenuTreeInfo.setMenuId(subSystemMenuDO.getId());
                        subMenuTreeInfo.setLabel(subSystemMenuDO.getMenuName());
                        subMenuTreeInfos.add(subMenuTreeInfo);
                    });
                    menuTreeInfo.setChildren(subMenuTreeInfos);
                } else {
                    menuTreeInfo.setChildren(Collections.emptyList());
                }
                menuTreeInfos.add(menuTreeInfo);
            });
            return menuTreeInfos;
        }
        return Collections.emptyList();
    }


    /**
     * 将 List<UserSystemMenu> 转换为 List<UserMenuInfo>
     *
     * @author: huangsheng
     * @date: 2017/10/20 下午2:00
     */
    public static List<UserMenuInfo> userSystemMenusConUserMenuInfo(final List<UserSystemMenu> userSystemMenus) {
        if (null != userSystemMenus && userSystemMenus.size() > 0) {
            List<UserMenuInfo> userMenuInfos = new ArrayList<>();
            userSystemMenus.forEach(systemMenuTree -> {
                UserMenuInfo cmsMenuInfo = new UserMenuInfo();
                cmsMenuInfo.setName(systemMenuTree.getMenuName());
                cmsMenuInfo.setPath(systemMenuTree.getMenuUrl());
                // 二级菜单转换
                List<SubSystemMenu> subSystemMenus = systemMenuTree.getSubSystemMenus();
                if (null != subSystemMenus && subSystemMenus.size() > 0) {
                    List<UserSubMenuInfo> subMenuInfos = new ArrayList<>();
                    subSystemMenus.forEach(subSystemMenu -> {
                        subMenuInfos.add(SystemMenuConvertor.subSystemMenuConUserSubMenuInfo(subSystemMenu));
                    });
                    cmsMenuInfo.setChildren(subMenuInfos);
                }
                userMenuInfos.add(cmsMenuInfo);
            });
            return userMenuInfos;
        }
        return Collections.emptyList();
    }

    /**
     * 将 SubSystemMenu 转换为 UserSubMenuInfo
     *
     * @author: huangsheng
     * @date: 2017/10/20 下午2:00
     */
    protected static UserSubMenuInfo subSystemMenuConUserSubMenuInfo(final SubSystemMenu subSystemMenu) {
        if (null == subSystemMenu) {
            return null;
        }
        UserSubMenuInfo subMenuInfo = new UserSubMenuInfo();
        subMenuInfo.setName(subSystemMenu.getMenuName());
        subMenuInfo.setPath(subSystemMenu.getMenuUrl());
        return subMenuInfo;
    }
}
